<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 14:46
 */

class Involved_ProductModel extends BaseModel
{
    protected $table = 'product';

    /**************************API端***********************/

    /**
     * 商城产品列表
     * @param $paramObj
     * @return mixed
     */
    public function GetApiProductList($paramObj)
    {
        $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;

        $db = DB::table($this->table);

        #筛选
        //关键字
        if (!empty($paramObj->key_word)) {
            $db->where('product_name', 'like', "%{$paramObj->key_word}%");
        }

       //分类
        if (property_exists($paramObj,'category') && !empty($paramObj->category) && $paramObj->category != 'all') {
            $db->where('category', $paramObj->category);
        }
        //品类
        if (property_exists($paramObj,'classes') && !empty($paramObj->classes) && $paramObj->classes != 'all') {
            $db->where('classes', $paramObj->classes);
        }
        //材质
        if (property_exists($paramObj,'quality') && !empty($paramObj->quality) && $paramObj->quality != 'all') {
            $db->where('quality', $paramObj->quality);
        }
        //规格
        if (property_exists($paramObj,'spec') && !empty($paramObj->spec) && $paramObj->spec != 'all') {
            $db->where('spec', $paramObj->spec);
        }
        //等级
        if (property_exists($paramObj,'level') && !empty($paramObj->level) && $paramObj->level != 'all') {
            $db->where('level', $paramObj->level);
        }
        //产地
        if (property_exists($paramObj,'origin') && !empty($paramObj->origin) && $paramObj->origin != 'all') {
            $db->where('place', $paramObj->origin);
        }
        //仓储地
        if (property_exists($paramObj,'storage') && !empty($paramObj->storage) && $paramObj->storage != 'all') {
            $db->where('storage', 'like', "%{$paramObj->storage}%");
        }
        //品牌
        if (property_exists($paramObj,'brand') && !empty($paramObj->brand) && $paramObj->brand != 'all') {
            $db->where('brand', $paramObj->brand);
        }


      #排序
        if ($paramObj->sort_order == 'all') {
            $db->orderBy('created', 'desc');
        }
        if ($paramObj->sort_order == 'price_desc') {
            $db->orderBy('price', 'desc');
        }
        if ($paramObj->sort_order == 'price_asc') {
            $db->orderBy('price', 'asc');
        }
        if ($paramObj->sort_order == 'sales') {
            $db->orderBy('sales', 'desc');
        }
        #查询
        $goods =  $db
            ->select('product_id','product_name','product_img','spec','place','price','classes','category','storage','unit')
            ->where('stock', '!=', 0)
            ->where('is_up', '!=', 0)
            ->where('status',1)
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();


        return $goods;

    }

    /**
     * 删除库存商品
     * @param $product_id
     * @param $supplier_id
     * @return mixed
     */
    public function DeleteStoreProduct($product_id, $supplier_id)
    {
        return  DB::table($this->table)
            ->where('product_id',$product_id)
            ->where('supplier_id',$supplier_id)
            ->delete();

    }

    /**
     * 获取商品名称
     * @param $product_id
     * @return string
     */
    public function getNameById($product_id)
    {
        $product_Info = DB::table($this->table)
            ->select('product_name')
            ->where('product_id',$product_id)
            ->first();
        if (empty($product_Info))
            return '';

        return $product_Info->product_name;
    }

    /**
     * 所有产品列表
     * @return info
     */
    public function GetProductList()
    {
        return  DB::table($this->table)
                ->select('product_name','spec','place','stock','shop_price','product_img1','level','classes')
                ->get();
    }

    /**
     * 根据ID获取产品详情
     * @param $product_id
     * @return mixed
     */
    public function GetProductById($product_id)
    {
        return  DB::table($this->table)
            ->select('product.product_name','product.product_sn','product.level','product.unit','product.supplier_id','product.description',
                'product.product_img','product.place', 'product.storage','product.price','product_cate.cate_name','product.stock','product.classes','product.phone',
                'supplier.supplier_name',
                'product.spec as spec_name'
            )
            ->leftJoin('supplier', 'supplier.supplier_id', '=', 'product.supplier_id')
            ->leftJoin('product_cate', 'product_cate.cate_id', '=', 'product.quality')
         /*   ->leftJoin('spec', 'spec.spec_id', '=', 'product.spec')*/
         /*   ->leftJoin('level', 'level.level_id', '=', 'product.level')*/
            ->where('product.product_id',$product_id)
            ->first();
    }

    /**
     * 产品详情
     * @param $product_id
     * @author fyl
     * @date 2016-1-25
     * @return mixed
     */
    public function GetProductDetail($product_id)
    {
        return  DB::table($this->table)
                ->select('product.supplier_id','product.product_id','product.product_sn','product.level','product.product_name','product.classes','product.price','supplier.company as company', 'product.unit','supplier.phone as phone',
                    'product.spec','product.storage','product.place','product.stock',
                    'product.start_num','supplier.brief','product.description',
                       'product_cate.cate_name',
                       'product_pic.show_img1','product_pic.show_img2','product_pic.show_img3')
                ->leftJoin('supplier', 'supplier.supplier_id', '=', 'product.supplier_id')
               ->leftJoin('product_cate', 'product_cate.cate_id', '=', 'product.category')
            ->leftJoin('product_pic', 'product_pic.product_id', '=', 'product.product_id')
                ->where(['product.product_id' => $product_id])
                ->first();
    }

    public function GetProductBoard()
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id, sc_product.product_name,sc_product.price, sc_product.stock,
            sc_product.product_img,sc_product.is_up,sc_cate1.cate_name AS category,sc_cate2.cate_name AS classes,
            sc_cate3.cate_name AS quality, sc_product_brand.brand_name, sc_supplier.supplier_name')
            ->leftJoin('product_cate as sc_cate1', 'cate1.cate_id', '=', 'product.category')
            ->leftJoin('product_cate as sc_cate2', 'cate2.cate_id', '=', 'product.classes')
            ->leftJoin('product_cate as sc_cate3', 'cate3.cate_id', '=', 'product.quality')
            ->leftJoin('product_brand', 'product_brand.brand_id', '=', 'product.brand_id')
            ->leftJoin('supplier', 'supplier.supplier_id', '=', 'product.supplier_id')
            ->get();
    }

    /**
     * 商家产品列表
     * @param $supplier_id
     * @param $page
     * @return mixed
     * @author alice
     * @date 2016-1-26
     */
    public function GetSupplierProductList($supplier_id, $page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
                ->select('product_id','product_name','spec','place','price','stock','product_img','status','classes')
                ->where('supplier_id',$supplier_id)
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
    }

    /**
     * 上下架产品列表
     * @param $supplier_id
     * @param $op
     * @param $page
     * @return mixed
     * @author fyl
     * @date 2016-5-13
     */
    public function GetManageProduct($supplier_id, $op,$page,$status)
    {
        if($status!=2){
            $offset = ($page-1) * HostEnum::PAGE_NUM;
            return DB::table($this->table)
                ->select('product_id','product_name','place','price','product_img','is_up','created','stock','spec')
                ->where(['is_up' => $op, 'supplier_id' => $supplier_id])
                ->where('status',1)
                ->orderBy('created','desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
        }else{
            $offset = ($page-1) * HostEnum::PAGE_NUM;
            return DB::table($this->table)
                ->select('product_id','product_name','place','price','product_img','is_up','created','stock','spec')
                ->where(['is_up' => $op, 'supplier_id' => $supplier_id])
                ->where('status','!=',1)
                ->orderBy('created','desc')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
        }

    }

    /**
     * 首页产品列表
     * @return mixed
     * @author fyl
     * @date 2016-5-1
     */
    public function GetIndexData()
    {
        return  DB::table($this->table)
                ->select('product_name','thumb_img1','spec','level','stock','shop_price')
                ->where('stock', '!=', 0)
                ->where('is_up', '!=', 0)
                ->where('status',1)
                ->get();
    }

    /**
     * 首页展示列表
     * @param $inWhere
     * @param $page
     * @author fyl
     * @date 2016-5-21
     * @return mixed
     */
    public function GetSelfSupportList($inWhere, $page)
    {
        #分页
        $page = empty($page)?1:$page;
        $offset = ($page-1) * HostEnum::PAGE_NUM;

        #查询语句
        $oQuery = DB::table($this->table)->select('product_id','product_name','product_img','spec','price','supplier_id','created',''); //字段
        /*if($inWhere == 'index') //条件
            $oQuery->where(['is_index' => ProductEnum::PRODUCT_INDEX_YES, 'is_self_support' => ProductEnum::PRODUCT_INDEX_YES]);*/
       /* $oQuery->orderBy('created','desc');*/
        $oQuery->where(['is_up'=>1,'status'=>1]);
        $oQuery->orderBy('created','desc');
        $info = $oQuery ->skip($offset)->take(HostEnum::PAGE_NUM)->get(); //结果

        return $info;

    }

    /**
     * 自营产品列表
     * @param $service_type
     * @param $page
     * @author fyl
     * @date 2016-6-22
     * @return mixed
     */
    public function GetSelfSupportProduct($service_type,$page)
    {
        if($page == 1){
            $offset = ($page-1);}else{
            $offset = ($page-1) * HostEnum::PAGE_NUM;
        }
         return DB::table($this->table)
                ->select('product_id','product_name','product_img','level','spec','place','stock','price','category')
                ->where(['is_self_support'=> 1, 'channel_id'=>$service_type])
                ->where('stock', '!=', 0)
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();

    }

    /**
     * 首页自营产品列表
     * @author fyl
     * @date 2016-6-22
     * @return mixed
     */
    public function GetindexList($cate,$limit)
    {
        return DB::table($this->table)
            ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
            ->where(['category'=>$cate])
            ->where('stock', '!=', 0)
            ->orderBy('product_id','desc')
            ->take($limit)
            ->get();

    }

    /**
     * 商城列表页
     * @author
     * @date 2016-6-22
     * @return mixed
     */

    public function Shopping($paramObj)
    {
//        $offset = ($page-1) * 12;
       $db = DB::table($this->table);
        #筛选
        //关键字
        if (!empty($paramObj->one)) {
            $db->where('category',$paramObj->one);
        }

        if (!empty($paramObj->two)) {
            $db->where('classes',$paramObj->two);
        }
        if (!empty($paramObj->three)) {
            $db->where('quality',$paramObj->three);
        }
        if (!empty($paramObj->storage)) {
            $db->where('storage',$paramObj->storage);
        }
        #排序
        if ($paramObj->price == 0) {
            $db->orderBy('created', 'desc');
       }
        if ($paramObj->price == 1) {
            $db->orderBy('price', 'asc');
        }
        if ($paramObj->price == 2) {
           $db->orderBy('price', 'desc');
        }
        if (!empty($paramObj->name)) {
            $db->where('product_name', 'like', "%{$paramObj->name}%");
        }
        if (!empty($paramObj->category)) {
            $db->where('category',$paramObj->category);
        }
        if (!empty($paramObj->quality)) {
            $db->where('quality',$paramObj->quality);
        }
        #查询
        $goods =  $db
            ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
            ->where('stock', '!=', 0)
        //    ->where(['is_self_support'=> 1])
            ->where('is_up', '!=', 0)
            ->where('status','=',1)
            ->orderBy('product_id','desc')
            ->get();
             return $goods;

    }
    public function Shopping_Index1($category){
        $db = DB::table($this->table);
        if (!empty($category)) {
            $db->where('category',$category);
        }
        return  $db
            ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
            ->where('stock', '!=', 0)
         //   ->where(['is_self_support'=> 1])
            ->where('is_up', '!=', 0)
            ->where('status','=',1)
            ->orderBy('product_id','desc')
            ->get();


    }
    public function Shopping_Index2($quality){
        $db = DB::table($this->table);
        if (!empty($quality)) {
            $db->where('quality',$quality);
        }
            return  $db
                ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
                ->where('stock', '!=', 0)
             //   ->where(['is_self_support'=> 1])
                ->where('is_up', '!=', 0)
                ->where('status','=',1)
                ->orderBy('product_id','desc')
                ->get();


    }
    public function ShoppingNull(){
        $db = DB::table($this->table);
        #查询
        $goods =  $db
            ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
            ->where('stock', '!=', 0)
        //    ->where(['is_self_support'=> 1])
            ->where('is_up', '!=', 0)
            ->where('status','=',1)
            ->orderBy('product_id','desc')
            ->get();
        //    ->paginate(12);
        return $goods;

    }
    #四大分类
    public function ShoppingFour($cate){
        return DB::table($this->table)
            ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
            ->where(['is_self_support'=> 1, 'category'=>$cate])
            ->where('stock', '!=', 0)
            ->orderBy('product_id','desc')
            ->paginate(12);



    }
    #关键字搜索
    public function SelectKeyword($name){
        return DB::table($this->table)
            ->select('product_id','product_name','product_img','level','spec','place','stock','price','unit','category')
            ->where('stock', '!=', 0)
        //    ->where(['is_self_support'=> 1])
            ->where('product_name', 'like', "%{$name}%")
            ->where('is_up', '!=', 0)
            ->where('status','=',1)
            ->orderBy('product_id','desc')
         //   ->paginate(12);
        ->get();
    }



    #商城原木板材详情页
    public function ShoppingDetails($product_id){
        return DB::table($this->table)
            ->select('product.product_id','product.product_name','product.product_sn','product.product_img','product.level',
                'product.spec','product.place','product.stock','product.price','product.unit','product.category',
                'product.start_num','product.storage','product.brief','product.description','product_pic.show_img1','product_pic.show_img2',
                'product_pic.show_img3')
            ->leftJoin('product_pic','product.product_id','=','product_pic.product_id')
            ->where('product.stock', '!=', 0)
            ->where('product.product_id',$product_id )
            ->orderBy('product.product_id','desc')
            ->get();



    }
    #商城家居工艺品详情页
    public function ShoppingDetails2($product_id){
        return DB::table($this->table)
            ->select('product.product_id','product.product_name','product.product_sn','product.product_img',
                'product.spec','product.stock','product.price','product.unit','product.category',
               'product.storage','product.quality','product.brief','product_pic.show_img1','product_pic.show_img2',
                'product_pic.show_img3')
            ->leftJoin('product_pic','product.product_id','=','product_pic.product_id')
            ->where('product.stock', '!=', 0)
            ->where('product.product_id',$product_id )
            ->orderBy('product.product_id','desc')
            ->get();



    }

    /**
     * 订单产品列表
     * @param $product_id
     * @return mixed
     * @author alice
     * @date 2016-2-19
     */
    public function GetOrderProductList($product_id)
    {
        return  DB::table($this->table)
            ->select('product_id','product_name','product_img','spec','price','place')
            ->where(['product_id'=>$product_id])
            ->get();
    }

    public function GetOrderProductList1($product_id,$order_id)
    {
        return  DB::table($this->table)
            ->select('product.product_id','product.product_name','product.product_img','product.spec','product.price','product.place','product.unit','product.storage')
            ->LeftJoin('order_product','product.product_id','=','order_product.product_id')
            ->where(['product.product_id'=>$product_id,'order_product.order_id'=>$order_id])
            ->get();
    }
    public function GetOrderProductList2($product_id)
    {
        return  DB::table($this->table)
            ->select('product_id','product_name','product_img','description','storage','phone')
            ->where(['product_id'=>$product_id])
            ->get();
    }

    /**************************PC端***********************/

    /**
     * 产品列表
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function ShowGoodsList()
    {
        return  DB::table($this->table)->paginate(8);
    }

    /**
     * 产品详情
     * @param $id
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function GetGoodsInfo($id)
    {
        return  DB::table($this->table)->where('product_id',$id)->first();
    }

    /**
     * 获取产品列表
     * @author fyl
     * @date 2016-04-04
     */
    public function AdminProductList()
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id, sc_product.product_name, sc_product.price, sc_supplier.supplier_name, sc_product.is_up,
            sc_cate1.cate_name as category, sc_product.product_img ')
            ->leftJoin('supplier','supplier.supplier_id','=','product.supplier_id')
            ->leftJoin('product_cate as sc_cate1', 'cate1.cate_id', '=', 'product.category')
            ->orderBy('product.created','desc')
            //->orderBy('product.length','asc')
            ->get();
    }

    /**
     * 获取产品审核列表
     * @author fyl
     * @date 2016-04-04
     */
    public function ProductExamineList()
    {
        return DB::table($this->table)
            ->selectRaw('sc_product.product_id, sc_product.product_name, sc_product.product_img,sc_supplier.supplier_name,
            sc_cate1.cate_name as category,sc_product.status')
            ->leftJoin('product_cate as sc_cate1', 'cate1.cate_id', '=', 'product.category')
            ->leftJoin('supplier','supplier.supplier_id','=','product.supplier_id')
            ->where('product.status',2)
            ->orderBy('product.created','desc')
            ->get();
    }



    /**
     * 根据ID获取产品详情(审核)
     * @return mixed
     */
    public function AdminGetProductById($id)
    {
        return  DB::table($this->table)
            ->selectRaw('sc_product.product_id, sc_product.product_name,sc_product.level, sc_product.price,sc_product.category,sc_product.unit,sc_product.spec,sc_product.stock,sc_product.description,sc_product.place,sc_product.product_sn,sc_product.storage,sc_product.phone,sc_product.start_num, sc_supplier.supplier_name,
            sc_product_cate.cate_name,sc_product.classes,sc_product.quality, sc_product_pic.show_img1,sc_product_pic.show_img2,sc_product_pic.show_img3')
            ->where('product.product_id',$id)
            ->leftJoin('product_cate', 'product_cate.cate_id', '=', 'product.category')
            ->leftJoin('supplier','supplier.supplier_id','=','product.supplier_id')
            ->leftjoin('product_pic','product_pic.product_id','=','product.product_id')
            ->first();
    }
    /**
     * 根据brand_ID获取该品牌下的商品
     * @return mixed
     */
    public function GetProductBrand($id)
    {
        return  DB::table($this->table)
            ->where('product.brand_id',$id)
            ->first();
    }
    //库存返回
    public function kcupdate($product_id,$number)
    {
        return  DB::table($this->table)->where('product_id',$product_id) ->increment('stock', $number);
    }

    /**
     * @param $id
     * @return array
     *获取推荐商品详情
     */
    public function AdProductInfo($id){
        return  DB::table($this->table)
            ->select('product_id','product_name','unit','price','spec','product_img')
            ->whereIn('product_id',$id)
            ->get();
    }

}